const App = {
    data() {
        return {
            input: "", //输入框
            treeData: [], //树的数据
            originalTreeData: [], //原始树的数据
            originalTableData: [], // 原始表格的数据
            columns: [], //表格的列
            loading: false, //表数据的懒加载
            showDialog: false, //设置字段弹窗
            checkedFields: [], //选中的字段
            isDataReady: false,
            Tabletdata: [], //表格的数据
            fields: [], //字段列表
            value: "",
            Total: 0,
            page:{
                PageNumber: 1, //页码
                RowAmount: 10,
            },
            
        };
    },
    mounted() {
        this.Treedata();
        this.getList();
    },
    computed: {
        // 计算属性，返回选中的字段
        isAllChecked() {
            return this.checkedFields.length === this.fields.length;
        },
    },
    methods: {
        // 树删除
        remove(node, data) {
            data.Row_Status = "1"
            // 这里添加表单提交逻辑
            let A = new Array()  //创建一个数组A
            A.push(data)    //将form表单添加到A中
            let B = new Object() //创建一个对象B
            B.grid_information = A  //将A赋给B的grid_information（名字可以自定义）属性

            let C = new Object()  //创建一个对象C
            C.Modify = B             //将B赋给C的Add属性
            const _this = this
            $.ajax({
                headers: requestHeaders(),
                url: "https://service.essp.top/apiForMoHe/generalBusiness/generalQueryByTableNameWithOriginal",
                method: "post",
                data: {
                    Data: "agricultural_products", //表名
                    Condition: "Agricultural_Products_SerialNumber ='" + data.Agricultural_Machinery_SerialNumber + "'",
                },
                success: (res) => {
                    console.log('成功的', res);
                    // res[0].Row_Status = "1"
                    console.log('xiugaizzhi', res[0].Row_Status = "1");
                    let A = new Array()  //创建一个数组A
                    A.push(res[0])    //将form表单添加到A中
                    console.log(A)
                    let B = new Object() //创建一个对象B
                    B.grid_information = A  //将A赋给B的grid_information（名字可以自定义）属性
                    console.log(B)
                    let C = new Object()  //创建一个对象C
                    C.Modify = B             //将B赋给C的Add属性
                    console.log('删除的当大', C)
                    $.ajax({
                        headers: requestHeaders(),
                        url: "https://service.essp.top/apiForMoHe/generalBusiness/generalSave",
                        method: "post",
                        data: {
                            Data: JSON.stringify(C),
                        },
                        success: (res) => {
                            console.log(res);
                            console.log('uu', res);
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        error: (err) => {
                            console.log(err);
                        },
                    });

                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                error: (err) => {
                    console.log(err);
                },
            });
        },
        handleSizeChange(newSize) {
            this.page.PageNumber = 1;
            this.page.RowAmount = newSize;
            this.postgetList(this.page);
        },
        handleCurrentChange(newPage) {            
            this.page.PageNumber =newPage;
            this.postgetList(this.page);
        },
        //树的数据
        Treedata() {
            const _this = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/clientOperation/getDistrictTree`,
                type: "GET",
                data: {
                    DistrictCode: '232723'

                },
                success: (data) => {
                    console.log(data);
                    this.treeData = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        //获取表格的列
        getList() {
            const _this = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryAllColumnByTableName`,
                method: "get",
                data: {
                    Data: "agricultural_products",
                },
                success: (res) => {
                    console.log(res);
                    sessionStorage.setItem("TABLE_NAME", res[0].TABLE_NAME);
                    let PRI = res.filter((item) => {
                        return item.COLUMN_KEY === "PRI";
                    });
                    if (res.length > 0) {
                        let key = PRI[0].COLUMN_NAME;
                        console.log(key);
                        sessionStorage.setItem("key", key);
                    }
                    let data = res.filter((item) => {
                        return item.COLUMN_COMMENT;
                    });
                    console.log(data);

                    let Rdata = data.filter((item) => item.COLUMN_NAME !== "Row_Status");
                    console.log(Rdata);
                    _this.columns = Rdata;
                    _this.checkedFields = Rdata;
                    _this.fields = Rdata;
                    _this.isDataReady.value = true;
                    _this.loading = true;
                    _this.postgetList(_this.page);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                error: (err) => {
                    console.log(err);
                },
            });
        },
        //获取表格的数据
        postgetList(page) {
            let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
            const _this = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalPaginateQueryByTableName`,
                method: "post",
                data: {
                    Data: TABLE_NAME,
                    Condition: "Row_Status='0'",
                    ...page
                },
                success: (res) => {
                    _this.Total = res.Total;
                    _this.loading = false;
                    _this.Tabletdata = res.Rows;
                    _this.originalTableData = res.Rows; // 保存原始数据
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                error: (err) => {
                    console.log(err);
                },
            });
        },

        handleSelectionChange() { },
        //查询按钮
        check() {
            console.log(this.input);
            console.log(this.value);
            // 1. 过滤树数据
            try {
                if (this.input) {
                    this.treeData = this.filterTreeData(this.originalTreeData, this.input);
                } else {
                    this.treeData = this.originalTreeData;
                }
            } catch (error) {
                
            }
            
            // 2. 过滤表格数据
            let filteredData = [...this.originalTableData];
            // 根据下拉框的值过滤 Political_Affiliation
            if (this.value) {
                filteredData = filteredData.filter(
                    (item) => item.Crop_Name === this.value
                );
            }
            this.Tabletdata = filteredData;
            console.log("查询操作");
        },
        //重置按钮
        reset() {
            // 这里可以根据你的实际需求修改逻辑
            window.location.reload();
            console.log("重置操作");
        },
        //过滤树的数据
        filterTreeData(data, searchText) {
            return data.filter((item) => {
                if (item.label.includes(searchText)) {
                    return true;
                }
                if (item.children) {
                    const filteredChildren = this.filterTreeData(
                        item.children,
                        searchText
                    );
                    if (filteredChildren.length > 0) {
                        item.children = filteredChildren;
                        return true;
                    }
                }
                return false;
            });
        },
        //添加按钮
        personalAdds(arg_nodekey) {
            console.log('arg_nodekey', arg_nodekey.ProvinceNumber);
            window.location.href = "./TreedataAdd.html?nodeKey=" + arg_nodekey.ProvinceNumber + "&action=Add";
        },
        //编辑按钮
        handleEdits(arg_nodekey) {
            console.log('arg_nodekey', arg_nodekey);
            window.location.href = "./TreedataAdd.html?nodeKey=" + arg_nodekey.ProvinceNumber + "&action=Modify";
        },
        //添加按钮
        personalAdd() {
            window.location.href = "./add.html?action=Add";
        },
        //编辑按钮
        handleEdit(index, row) {
            window.location.href = "./add.html?nodeKey=" + row.Agricultural_Machinery_SerialNumber + "&action=Modify";
        },
        // 弹窗全选
        handleAllChecked(val) {
            console.log(val);
            console.log(val);
            let a; // 提前声明变量 a
            if (val === false) {
                console.log(this.fields);
                a = this.fields.filter((item) => {
                    return item.COLUMN_KEY == "PRI";
                });
                console.log(a);
            } else {
                a = [...this.fields]; // 当 val 为 true 时，给 a 赋值为 props.fields 的副本
            }
            // 使用 emit 更新 checkedFields
            this.checkedFields = a;
        },
        saveFields() {
            const selectedFields = this.fields.filter((item) => {
                return this.checkedFields.some(
                    (cItem) => cItem && cItem.COLUMN_NAME === item.COLUMN_NAME
                );
            });
            console.log("选中的字段:", selectedFields);
            // 更新 columns
            this.columns = selectedFields;
            this.showDialog = false;
        },
        //删除数据
        handleDelete(index, row) {
            let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
            console.log(TABLE_NAME);
            console.log(index, row);
            row.Row_Status = "1";
            let lArray = new Array();
            lArray.push(row);
            let lJSONObject = new Object();
            lJSONObject[TABLE_NAME] = lArray; //换表
            let lJSONObject_DataSet = new Object(); // Modify
            lJSONObject_DataSet.Modify = lJSONObject; //换表
            const _this = this;
            // return;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/clientOperation/generalSaveForClient`,
                method: "post",
                data: {
                    Data: JSON.stringify(lJSONObject_DataSet),
                },
                success: (res) => {
                    console.log(res);
                    _this.postgetList(_this.page);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                error: (err) => {
                    console.log(err);
                },
            });
        },
        //树的点击事件
        handleNodeClick(data) {
            console.log(data);
        },
        //显示字段弹窗
        handleOpenDialog() {
            this.showDialog = true;
        },
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus);
app.mount("#app");
